package com.kevin.S2SM.log;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.jdbc.JDBCAppender;

public class DBAppender extends JDBCAppender {

	private static Logger logger = Logger.getLogger(DBAppender.class);

	private Connection connection = null;

	public DBAppender() throws ClassNotFoundException {
	}

	@Override
	protected Connection getConnection() throws SQLException {
		try {
			Properties jdbcProperty = new Properties();
			try {
				jdbcProperty.load(DBAppender.class.getClassLoader()
						.getResourceAsStream("jdbc.properties"));

				Class.forName(jdbcProperty.getProperty("jdbcDriver"));

				String url = jdbcProperty.getProperty("url");
				String userName = jdbcProperty.getProperty("userName");
				String password = jdbcProperty.getProperty("password");
				this.connection = DriverManager.getConnection(url, userName,
						password);

				return connection;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void closeConnection(Connection con) {
		// TODO Auto-generated method stub
		try {
			connection.close();
			connection = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("连接没正常关闭", e);

		}
	}

}
